package com.ruoyi.project.wf.service.impl;

import java.util.List;

import com.ruoyi.common.constant.PaeaConstants;
import com.ruoyi.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.wf.domain.WfProcessDef;
import com.ruoyi.project.wf.enums.WfProcessInstStatus;
import org.springframework.stereotype.Service;
import com.ruoyi.project.wf.mapper.WfProcessInstMapper;
import com.ruoyi.project.wf.domain.WfProcessInst;
import com.ruoyi.project.wf.service.IWfProcessInstService;
import com.ruoyi.common.utils.text.Convert;

import javax.annotation.Resource;

/**
 * 流程实例信息Service业务层处理
 *
 * @author XQ.XU
 * @date 2020-03-20
 */
@Service
public class WfProcessInstServiceImpl extends ServiceImpl<WfProcessInstMapper, WfProcessInst> implements IWfProcessInstService
{
    @Resource
    private WfProcessInstMapper wfProcessInstMapper;

    /**
     * 查询流程实例信息
     *
     * @param id 流程实例信息ID
     * @return 流程实例信息
     */
    @Override
    public WfProcessInst selectWfProcessInstById(String id)
    {
        return wfProcessInstMapper.selectWfProcessInstById(id);
    }

    /**
     * 查询流程实例信息列表
     *
     * @param wfProcessInst 流程实例信息
     * @return 流程实例信息
     */
    @Override
    public List<WfProcessInst> selectWfProcessInstList(WfProcessInst wfProcessInst)
    {
        return wfProcessInstMapper.selectWfProcessInstList(wfProcessInst);
    }

    /**
     * 新增流程实例信息
     *
     * @param wfProcessInst 流程实例信息
     * @return 结果
     */
    @Override
    public int insertWfProcessInst(WfProcessInst wfProcessInst)
    {
        wfProcessInst.setCreateTime(DateUtils.getNowDate());
        return wfProcessInstMapper.insertWfProcessInst(wfProcessInst);
    }

    /**
     * 修改流程实例信息
     *
     * @param wfProcessInst 流程实例信息
     * @return 结果
     */
    @Override
    public int updateWfProcessInst(WfProcessInst wfProcessInst)
    {
        wfProcessInst.setUpdateTime(DateUtils.getNowDate());
        return wfProcessInstMapper.updateWfProcessInst(wfProcessInst);
    }

    /**
     * 删除流程实例信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWfProcessInstByIds(String ids)
    {
        return wfProcessInstMapper.deleteWfProcessInstByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除流程实例信息信息
     *
     * @param id 流程实例信息ID
     * @return 结果
     */
    @Override
    public int deleteWfProcessInstById(String id)
    {
        return wfProcessInstMapper.deleteWfProcessInstById(id);
    }

    public WfProcessInst createWfProcessInst(WfProcessDef wfProcessDef) {
        // 启动一个流程实例
        WfProcessInst wfProcessInst = new WfProcessInst();
        wfProcessInst.setBusiType(PaeaConstants.SYS_CODE);
        wfProcessInst.setCreateTime(new java.sql.Date(System.currentTimeMillis()));
        wfProcessInst.setCreatorCode(ShiroUtils.getUserId());
        wfProcessInst.setCreatorName(ShiroUtils.getLoginName());
        wfProcessInst.setStatus(WfProcessInstStatus.Created.getValue());
        wfProcessInst.setWfProcessDef(wfProcessDef);
        wfProcessInst.setProcDefId(wfProcessDef.getId());
        wfProcessInst.setProcCode(wfProcessDef.getCode());
        wfProcessInst.setProcName(wfProcessDef.getName());
        saveOrUpdate(wfProcessInst);
        // 产生一个流程实例
        return wfProcessInst;
    }
}
